package com.example.backend.mapper;

import com.example.backend.pojo.Restaurant;

import com.example.backend.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;


@Mapper
public interface RestaurantMapper {

    @Select("select * from restaurant where restaurantname=#{restaurantname}")
    Restaurant getRestaurantByName(String restaurantname);

    @Select("select * from restaurant where restaurantid=#{restaurantid}")
    Restaurant getRestaurantById(String restaurantid);

    @Update("update restaurant set addressareaid=#{addressareaid},restauranttypename=#{restauranttypename},phone=#{phone},image=#{image},introduction=#{introduction},addressdetail=#{addressdetail},updatetime=now(),qualification=#{qualification},surplus=#{surplus} where restaurantid=#{restaurantid}")
    void update(Restaurant restaurant);

    @Update("update restaurant set password=#{new_pwd} ,updatetime=now() where restaurantid=#{id}")
    void updatePwd(@Param("id")Integer id, @Param("new_pwd")String new_pwd);

    @Insert("insert into restaurant (restaurantname,password,createtime,updatetime,image,addressareaid,restauranttypename,phone,introduction,addressdetail,surplus,state,qualification,point,sales) values (#{restaurantname},#{password},now(),now(),#{image},#{addressareaid},#{restauranttypename},#{phone},#{introduction},#{addressdetail},#{surplus},0,#{qualification},5,0)")
    void add(Restaurant restaurant);

    @Select("select * from restaurant")
    List<Restaurant> getall();

    @Update("update restaurant set surplus=#{surplus} ,updatetime=now() where restaurantid=#{id}")
    void surplus(@Param("id")String id,@Param("surplus")Integer surplus);

    @Select("select * from restaurant where restaurantname like CONCAT('%',#{restaurantname},'%') ")
    List<Restaurant> search(String restaurantname);
    @Update("update restaurant set sales=#{sales},updatetime=now() where restaurantid=#{restaurantid}")
    void updatesales(@Param("sales")Integer sales,@Param("restaurantid")String restaurantid);

    @Update("update restaurant set point=#{point} where restaurantid=(SELECT restaurantid from foodorder where orderid=#{orderid})")
    void updatepoint (@Param("point")Float point,@Param("orderid")Integer orderid);


    @Update("update restaurant set state=2,updatetime=now() where restaurantid=#{restaurantid}")
    void relax(Integer restaurantid);
    @Update("update restaurant set state=1,updatetime=now() where restaurantid=#{restaurantid}")
    void normal(Integer restaurantid);
    @Select("select * from restaurant where state=0")
    List<Restaurant> getallnormalres();
    @Select("SELECT user.username, user.image, user.phone, user.age, user.gender, COUNT(foodorder.userid) AS number FROM foodorder INNER JOIN user ON foodorder.userid = user.userid WHERE foodorder.restaurantid = #{restaurantid} GROUP BY user.userid DESC")
    List<User> getclient(String restaurantid);


    @Select("SELECT restaurant.addressareaid,restaurant.addressdetail,restaurant.createtime,restaurant.image,restaurant.introduction,restaurant.phone,restaurant.point,restaurant.qualification,restaurant.restaurantid,restaurant.restaurantname,restaurant.restauranttypename,restaurant.state,restaurant.sales,restaurant.surplus,restaurant.updatetime  FROM usertype,restaurant\n" +
            "WHERE restaurantid=`first` and usertypeid=#{usertypeid} \n" +
            "UNION \n" +
            "SELECT restaurant.addressareaid,restaurant.addressdetail,restaurant.createtime,restaurant.image,restaurant.introduction,restaurant.phone,restaurant.point,restaurant.qualification,restaurant.restaurantid,restaurant.restaurantname,restaurant.restauranttypename,restaurant.state,restaurant.sales,restaurant.surplus,restaurant.updatetime FROM usertype,restaurant\n" +
            "WHERE restaurantid=second and usertypeid=#{usertypeid} \n" +
            "UNION \n" +
            "SELECT restaurant.addressareaid,restaurant.addressdetail,restaurant.createtime,restaurant.image,restaurant.introduction,restaurant.phone,restaurant.point,restaurant.qualification,restaurant.restaurantid,restaurant.restaurantname,restaurant.restauranttypename,restaurant.state,restaurant.sales,restaurant.surplus,restaurant.updatetime FROM usertype,restaurant\n" +
            "WHERE restaurantid=third and usertypeid=#{usertypeid} \n" +
            "UNION \n" +
    "SELECT restaurant.addressareaid,restaurant.addressdetail,restaurant.createtime,restaurant.image,restaurant.introduction,restaurant.phone,restaurant.point,restaurant.qualification,restaurant.restaurantid,restaurant.restaurantname,restaurant.restauranttypename,restaurant.state,restaurant.sales,restaurant.surplus,restaurant.updatetime   from user,foodorder,restaurant where user.userid=foodorder.userid and user.userid= #{userid} and foodorder.restaurantid=restaurant.restaurantid GROUP BY restaurantid")
    List<Restaurant> reccommendation(@Param("usertypeid")Integer usertypeid,@Param("userid")Integer userid);


    @Select("select restaurantid from restaurant where restaurantname=#{restaurantName}")
    String getRestaurantIdByName(String restaurantName);

    @Update("update restaurant set inform=#{inform},state=3 where restaurantid=#{restaurantid} ")
    void rejectres(String inform,String restaurantid);

    @Update("update restaurant set inform=#{inform} where restaurantid=#{restaurantid} ")
    void addinform(String inform,String restaurantid);

    @Update("update restaurant set money=money+#{money} where restaurantid=#{restaurantid} ")
    void updatemoney(Double money,String restaurantid);

    @Select("select money from restaurant where restaurantid=#{restaurantid} ")
    Double getmoney(String restaurantid);
}
